// 这是关于文章的网络请求接口
import axios, { AxiosDefaultTypes } from '@/utils/axios'

import type { AxiosResponse } from 'axios'
import type { AxiosSearchListTypes } from './types'

export * from './types'

// 发表文章
/**
 *
 * @param articleInfo { 作者ID, 文章标题，文章简介，文章类型，文章内容，文章封面 }
 *
 */
export const publishArticle: ({
  adminID,
  title,
  des,
  type,
  text,
  cover
}: {
  adminID: string
  title: string
  des: string
  type: number
  text: string
  cover?: string
}) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = ({
  adminID,
  title,
  des,
  type,
  text,
  cover
}) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/article/publish', {
    adminID,
    title,
    des,
    type,
    text,
    cover
  })
}

// 删除文章
/**
 *
 * @param articleID 文章ID
 *
 */
export const deleteArticle: (
  articleID: string
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  articleID: string
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/article/delete', {
    articleID
  })
}

// 修改文章 用户是否可见
/**
 *
 * @param articleID 文章ID
 */
export const updateArticleIsShow: (
  articleID: string
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  articleID: string
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/article/updatepower', {
    articleID
  })
}

// 修改文章信息
/**
 *
 * @param articleID 文章ID
 * @param title 文章标题
 * @param des 文章描述
 * @param cover 文章封面
 * @param type 文章类型
 * @param text 文章内容
 */
export const updateArticleInfo: (
  articleID: string,
  title: string,
  des: string,
  cover: string,
  type: number,
  text: string
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  articleID: string,
  title: string,
  des: string,
  cover: string,
  type: number,
  text: string
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/article/updateInfo', {
    articleID,
    title,
    des,
    cover,
    type,
    text
  })
}

// 搜索关键字查询文章列表
/**
 *
 * @param type 文章类型 0新闻资讯 1公益作用 2公益记录
 * @param name 文章标题关键字
 * @param limit 请求数据数量
 * @param skip 请求数据偏移量
 */
export const searchArticle: (
  type: number,
  name: string,
  limit: number,
  skip: number
) => Promise<AxiosResponse<AxiosDefaultTypes<AxiosSearchListTypes>, any>> = (
  type: number,
  name: string,
  limit: number,
  skip: number
) => {
  return axios.get<AxiosDefaultTypes<AxiosSearchListTypes>>(
    '/api/article/search',
    {
      params: {
        type,
        name,
        limit,
        skip
      }
    }
  )
}
